package com.dds.system.service.union.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.dds.common.config.AppInterfaceConfig;
import com.dds.common.core.domain.AjaxResult;
import com.dds.common.core.domain.entity.SysUser;
import com.dds.common.core.redis.RedisCache;
import com.dds.common.utils.DateUtils;
import com.dds.common.utils.http.HttpUtils;
import com.dds.system.mapper.sys.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dds.system.mapper.union.UnionShopQrcodeMapper;
import com.dds.system.domain.union.UnionShopQrcode;
import com.dds.system.service.union.IUnionShopQrcodeService;

/**
 * 联盟商家的二维码Service业务层处理
 * 
 * @author gaolianjie
 * @date 2021-04-25
 */
@Service
public class UnionShopQrcodeServiceImpl implements IUnionShopQrcodeService 
{
    @Autowired
    private UnionShopQrcodeMapper unionShopQrcodeMapper;

    @Autowired
    private AppInterfaceConfig appInterfaceConfig;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询联盟商家的二维码
     * 
     * @param id 联盟商家的二维码ID
     * @return 联盟商家的二维码
     */
    @Override
    public UnionShopQrcode selectUnionShopQrcodeById(Integer id)
    {
        return unionShopQrcodeMapper.selectUnionShopQrcodeById(id);
    }

    /**
     * 查询联盟商家的二维码列表
     * 
     * @param unionShopQrcode 联盟商家的二维码
     * @return 联盟商家的二维码
     */
    @Override
    public List<Map<String, Object>> selectUnionShopQrcodeList(UnionShopQrcode unionShopQrcode, String shopName, Map<String, Object> time)
    {
        return unionShopQrcodeMapper.selectUnionShopQrcodeList(unionShopQrcode, shopName, time);
    }

    /**
     * 新增联盟商家的二维码
     * 
     * @param unionShopQrcode 联盟商家的二维码
     * @return 结果
     */
    @Override
    public int insertUnionShopQrcode(UnionShopQrcode unionShopQrcode)
    {
        unionShopQrcode.setCreateTime(DateUtils.getNowDate());
        return unionShopQrcodeMapper.insertUnionShopQrcode(unionShopQrcode);
    }

    /**
     * 修改联盟商家的二维码
     * 
     * @param unionShopQrcode 联盟商家的二维码
     * @return 结果
     */
    @Override
    public int updateUnionShopQrcode(UnionShopQrcode unionShopQrcode)
    {
        return unionShopQrcodeMapper.updateUnionShopQrcode(unionShopQrcode);
    }

    /**
     * 批量删除联盟商家的二维码
     * 
     * @param ids 需要删除的联盟商家的二维码ID
     * @return 结果
     */
    @Override
    public int deleteUnionShopQrcodeByIds(Integer[] ids)
    {
        return unionShopQrcodeMapper.deleteUnionShopQrcodeByIds(ids);
    }

    /**
     * 删除联盟商家的二维码信息
     * 
     * @param id 联盟商家的二维码ID
     * @return 结果
     */
    @Override
    public int deleteUnionShopQrcodeById(Integer id)
    {
        return unionShopQrcodeMapper.deleteUnionShopQrcodeById(id);
    }

    /**
     * 生成新的二维码
     * @param params 参数
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int createQr(Map<String, Object> params, Long userId) {
        int createCount = Integer.parseInt(params.get("createCount").toString());
        String createType = params.get("createType").toString();

        String res = HttpUtils.sendPost(
                appInterfaceConfig.getApiUrl() + "/rest/v1.0/back/qrcode/batch-create",
                "count=" + createCount + "&type=" + createType + "&userId=" + userId);

        Map resMap = JSONObject.parseObject(res, Map.class);

        if (Integer.parseInt(resMap.get("code").toString()) != 0) {
            return 0;
        } else {
            String path = "";
            List<Object> qrcodeLog;

            Map resDataMap = JSONObject.parseObject(resMap.get("data").toString(), Map.class);

            path += DateUtils.getTime();
            path += "^" + userId;
            path += "^" + createCount;
            path += "^" + resDataMap.get("download_url").toString();

            // 获取当前redis存储数据
            qrcodeLog = redisCache.getCacheList("qrcodeLog");
            if (qrcodeLog.size() > 0) {
                redisCache.deleteObject("qrcodeLog");
            }

            qrcodeLog.add(path);
            redisCache.setCacheList("qrcodeLog", qrcodeLog);

            return 1;
        }
    }

    @Override
    public Map<String, Object> getDownloadListForRedis(Long pageNum, Long pageSize) {
        long start = 0, end = pageSize - 1;
        if (pageNum > 1) {
            start = pageSize * pageNum - pageSize;
            end = start + pageSize - 1;
        }

        List qrcodeLog = redisCache.getCacheList("qrcodeLog", start, end);

        Map<String, Object> data = new HashMap<>();

        List<Map<String, Object>> rows = new ArrayList<>();
        for (Object log : qrcodeLog) {
            Map<String, Object> row = new HashMap<>();

            String[] split = log.toString().split("\\^");

            SysUser sysUser = sysUserMapper.selectUserById(Long.parseLong(split[1]));

            row.put("createTime", split[0]);
            row.put("createUser", sysUser.getNickName());
            row.put("createCount", split[2]);
            row.put("downloadUrl", split[3]);

            rows.add(row);
        }
        data.put("rows", rows);
        data.put("total", redisCache.getCacheCount("qrcodeLog"));

        return data;
    }
}
